package com.xuexiang.yoyo.sql.mapper;

import android.content.ContentValues;
import android.database.Cursor;

import androidx.annotation.NonNull;

import com.xuexiang.yoyo.constant.HesAppConstant;
import com.xuexiang.yoyo.constant.HesAppStatic;
import com.xuexiang.yoyo.entity.DataItemResult;
import com.xuexiang.yoyo.entity.LrDataItem;

import java.util.ArrayList;
import java.util.List;

public class LrDataItemMapper {
    public static final String TABLE = "lr_data_item";
    public static final String CREATE_TABLE_SQL="CREATE TABLE "+ TABLE +"(id INTEGER PRIMARY KEY AUTOINCREMENT," +
            "name TEXT ," +
            "short_name TEXT," +
            "ln TEXT," +
            "username TEXT," +
            "sort_index INTEGER," +
            "attribute_index INTEGER," +
            "gxdlms_class_name TEXT," +
            "cosem_class_id TEXT," +
            "attribute_indexs TEXT," +
            "fcat_tag TEXT," +
            "obis_define TEXT" +
            ");";



    public static void delete(){
        HesAppStatic.getmDataBaseOpenHelper().delete(TABLE,"username='" + HesAppStatic.getUsername() +"'",null);
    }

    public static void insert(LrDataItem item){
        ContentValues contentValues = new ContentValues();
        contentValues.put("name", item.getName());
        contentValues.put("short_name", item.getShortName());
        contentValues.put("ln", item.getLn());
        contentValues.put("username", item.getUsername());
        contentValues.put("sort_index", item.getSortIndex());
        contentValues.put("attribute_index", item.getAttrbuteIndex());
        contentValues.put("gxdlms_class_name", item.getGxdlmsClassName());
        contentValues.put("username", HesAppStatic.getUsername());
        contentValues.put("cosem_class_id", item.getCosemClassId());
        contentValues.put("attribute_indexs", item.getAttributeIndexs());
        contentValues.put("fcat_tag", item.getfcatTag());
        contentValues.put("obis_define", item.getObisDefine());
        HesAppStatic.getmDataBaseOpenHelper().insert(TABLE,contentValues);
    }
    public static List<LrDataItem> queryAll(){
        Cursor cursor = HesAppStatic.getmDataBaseOpenHelper().query(TABLE, "where username='" + HesAppStatic.getUsername() + "'");
        List<LrDataItem> resultList = getLrDataItems(cursor);
        cursor.close();
        return resultList;
    }

    @NonNull
    private static List<LrDataItem> getLrDataItems(Cursor cursor) {
        List<LrDataItem> resultList = new ArrayList<>();
        while (cursor.moveToNext()) {
            LrDataItem item = new LrDataItem();
            item.setId(cursor.getInt(0));
            item.setName(cursor.getString(1));
            item.setShortName(cursor.getString(2));
            item.setLn(cursor.getString(3));
            item.setUsername(cursor.getString(4));
            item.setSortIndex(cursor.getInt(5));
            item.setAttrbuteIndex(cursor.getInt(6));
            item.setGxdlmsClassName(cursor.getString(7));
            item.setCosemClassId(cursor.getString(8));
            item.setAttributeIndexs(cursor.getString(9));
            item.setfcatTag(cursor.getString(10));
            item.setObisDefine(cursor.getString(11));
            resultList.add(item);
        }
        return resultList;
    }

    public static List<LrDataItem> queryAllGroupLn(){
        Cursor cursor = HesAppStatic.getmDataBaseOpenHelper().query(TABLE, "where username='" + HesAppStatic.getUsername() + "' group by ln");
        List<LrDataItem> resultList = getLrDataItems(cursor);
        cursor.close();
        return resultList;
    }


    /**
     * 获取读取模板数据
     *
     * @return
     */
    public static List<DataItemResult>  getTemplateDataList() {
        List<DataItemResult> dataItemList = new ArrayList<>();
        //查询sqlite 获取结果进行查询
        List<LrDataItem> lrDataItems = queryAll();
        for (LrDataItem lrDataItem : lrDataItems) {
            DataItemResult item = new DataItemResult();
            item.setId(lrDataItem.getId());
            item.setName(lrDataItem.getShortName());
            item.setGxdlmsClassName(lrDataItem.getGxdlmsClassName());
            item.setLn(lrDataItem.getLn());
            item.setAttributeIndex(lrDataItem.getAttrbuteIndex());
            item.setConsemClassId(lrDataItem.getCosemClassId());
            item.setfcatTag(lrDataItem.getfcatTag());
            dataItemList.add(item);
        }


        if(dataItemList==null || dataItemList.size()==0){
            dataItemList.add(getDataItemResult(2,"Theshold - Voltage Sag","1.0.12.31.0.255","3","GXDLMSRegister", HesAppConstant.fcat_Tag_Parameters));
            dataItemList.add(getDataItemResult(3,"Theshold - Voltage Sag","1.0.12.31.0.255","3","GXDLMSRegister",HesAppConstant.fcat_Tag_Parameters));

            dataItemList.add(getDataItemResult(2,"Total Import Acitve Energy","1.0.1.8.0.255","3","GXDLMSRegister",HesAppConstant.fcat_Tag_Parameters));
            dataItemList.add(getDataItemResult(3,"Total Import Acitve Energy","1.0.1.8.0.255","3","GXDLMSRegister",HesAppConstant.fcat_Tag_Parameters));

            dataItemList.add(getDataItemResult(2,"Device ID 1- SN","0.0.96.1.0.255","1","GXDLMSData",HesAppConstant.fcat_Tag_Parameters));
            dataItemList.add(getDataItemResult(2,"COSEM LDN","0.0.42.0.0.255","1","GXDLMSData",HesAppConstant.fcat_Tag_Parameters));


            dataItemList.add(getDataItemResult(2,"Monthly Billing 1","0.0.98.1.0.255","7","GXDLMSProfileGeneric",HesAppConstant.fcat_Tag_Biling));
        }
        return dataItemList;
    }
    @NonNull
    private static DataItemResult getDataItemResult(int attributeIndex,String name,String ln,String classId,String className,String fcatTag) {
        DataItemResult dataItemResult = new DataItemResult();
        dataItemResult.setName(name);
        dataItemResult.setAttributeIndex(attributeIndex);
        dataItemResult.setLn(ln);
        dataItemResult.setConsemClassId(classId);
        dataItemResult.setGxdlmsClassName(className);
        dataItemResult.setfcatTag(fcatTag);
        return dataItemResult;
    }
}
